package mse.tx.arch.pipes;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import mse.FilterFramework;
import mse.FilterFramework.EndOfStreamException;

/******************************************************************************************************************
* File:SourceFilterTemplate.java
* Course: 17655
* Project: Assignment 1
* Copyright: Copyright (c) 2003 Carnegie Mellon University
* Versions:
*	1.0 November 2008 - Initial rewrite of original assignment 1 (ajl).
*
* Description:
*
* This class serves as a template for creating source filters. The details of threading, connections writing output
* are contained in the FilterFramework super class. In order to use this template the program should rename the class.
* The template includes the run() method which is executed when the filter is started. The run() method is the guts
* of the filter and is where the programmer should put their filter specific code.The run() method is the main
* read-write loop for reading data from some source and writing to the output port of the filter. This template
* assumes that the filter is a source filter that reads data from a file, device (sensor),or generates the data
* interally, and then writes data to its output port. In this case, only the output port is used. In cases where the
* filter is a standard filter or a sink filter, you should use the FilterTemplate.java or SinkFilterTemplate.java as
* a starting point for creating standard or sink filters.
*
* Parameters: 		None
*
* Internal Methods:
*
*	public void run() - this method must be overridden by this class.
*
******************************************************************************************************************/

public class FileReaderSourceFilter extends FilterFramework implements IConfigurable
{
	protected File inFile = null;
	

	@Override
	public void setConfiguration(Properties props)
	{
		if (props != null){
			setFilename(props.getProperty("filename"));
		}
	}
	public void setFilename(String filename)
	{
		if (filename != null)
			this.inFile = new File(filename);
	}
	
	
	public void run()
    {

		// Read file
		DataInputStream dataIn = null; 
		try {
			dataIn = new DataInputStream(
			        new FileInputStream(inFile));
			System.out.println( "\n" + this.getName() + "::File "+ inFile.getName()+" opened.");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		byte databyte = 0;

/*************************************************************
*	This is the main processing loop for the filter. Since this
*   is a source filter, the programer will have to determine
* 	when the loop ends.
**************************************************************/

		boolean eof = false;
		
		int byteCtr = 0;
		while ( !eof )
		{
			try {
				databyte = dataIn.readByte();
	           	WriteFilterOutputPort(databyte);
	           	byteCtr++ ;
			} catch (EOFException eofex) {
				eof = true;
			} catch (IOException e) {
				e.printStackTrace();
			}


		} // while

		System.out.println( "\n" + this.getName() + "::Finished read "+byteCtr+" bytes");
		if (dataIn != null) {
			try {
				dataIn.close();
				ClosePorts();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

   } // run

} // SourceFilterTemplate